﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;

namespace WPFExcelReport
{
    public partial class TBBBS13 : ViewBase
    {
        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
            typeof(string), typeof(TBBBS13),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBBBS13;
            if (src != null)
            {
                src.AUF = (string)e.NewValue;
            }
        }
        #endregion
        #region ==IsProduced===========
        public static readonly DependencyProperty IsProducedProperty = DependencyProperty.Register("IsProduced",
            typeof(bool), typeof(TBBBS13),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsProducedChanged));
        public bool IsProduced
        {
            get
            {
                return (bool)GetValue(IsProducedProperty);
            }
            set
            {
                SetValue(IsProducedProperty, value);
            }
        }
        private static void OnIsProducedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBBBS13;
            if (src != null)
            {
                src.IsProduced = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==BS===========
        public static readonly DependencyProperty BSProperty = DependencyProperty.Register("BS",
            typeof(string), typeof(TBBBS13),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBSChanged));
        public string BS
        {
            get
            {
                return (string)GetValue(BSProperty);
            }
            set
            {
                SetValue(BSProperty, value);
            }
        }
        private static void OnBSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBBBS13;
            if (src != null)
            {
                src.BS = (string)e.NewValue;
            }
        }
        #endregion
        public TBBBS13()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VBABEW();
            IsMasterFromMV = true;
            TBMain = MV.SetProd.Tables[TB];
            InitializeComponent();
            BS = "13";
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            Load();
        }

        public void Load()
        {
            switch (app.company)
            {
                default:
                case "JLT":
                case "YGR":
                    LoadJLT();
                    break;
            }
        }
        public void LoadJLT()
        {
            if (AUF == null)
                return;
            if (AUF.Length < 2)
                return;
            IsProduced = false;
            DataRow dwData = TBMain.NewRow();
            string ordertype = Helpers.Helper.Left(AUF, 1);

            dwData["BBMANDANT"] = app.Mandant;
            dwData["BBAUFKRS"] = AUF.Substring(1, 2);
            dwData["BBAUFLFD"] = AUF.Substring(3, 6);
            dwData["BBAUFPOS"] = AUF.Substring(9, 3);
            dwData["BBBS"] = BS;
            DataSet SResult = ((VBABEW)MV).QueryBABEW(dwData, 4);
            MergeData(SResult, false, true);

            int idx = 0;
            MessageInfo msgs = new MessageInfo();
            string msg = "";
            msgs.MSGTYPE = "BBBS13";
            msgs.Sender = this.RUNID;
            msgs.ACTION = "BBBS13";
            msgs.AUFTYP = "V";
            foreach (DataRow row in TBMain.Rows)
            {
                idx++;
                msgs.KRS = row["ABPAUFKRS"].ToString();
                msgs.LFD = row["ABPAUFLFD"].ToString();
                msgs.POS = row["ABPAUFPOS"].ToString();
                msgs.P1 = row["QTY"].ToString();
                msgs.P2 = Helper.Right(row["MDATE"].ToString(), 2);
                msgs.P3 = "4-浆纱";
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", dwMaster, msg, msgs);
            }
            if (idx > 0)
            {
                IsProduced = true;
            }
        }
    }
}
